<template>
  <div class="body">
    <div class="loginBox">
      <div class="userSelect">
          <div class="userBlock" style="background-color:#6F6F6FFF" @click="selectStudent" ref="xuesheng">学生登录</div>
          <div class="userBlock" @click="selectTeacher" ref="jiaoshi">教师登录</div>
          <div class="userBlock" @click="selectAdmin" ref="guanliyuan">管理员登录</div>
      </div>
      <div class="loginForm">
        <div style="margin: 20px;"></div>
        <el-form label-position="right" label-width="80px" :model="formLabelAlign" id="selectForm">

          <div class="inputBox">
            <el-form-item :label=this.accountType>
              <el-input v-model="formLabelAlign.username"></el-input>
            </el-form-item>
          </div>

          <div class="inputBox">
            <el-form-item label="密码">
              <el-input v-model="formLabelAlign.password" @keyup.enter.native="submit"></el-input>
            </el-form-item>
          </div>

        </el-form>

        <div class="summitBox">
<!--          <el-button @click="test">检测</el-button>-->
          <template v-if="!token">
            <el-button @click="submit">登录</el-button>
            <el-button @click="clearInput">重置</el-button>
          </template>
          <template v-if="token">
            <el-button @click="enterMain" style="width:300px">已登录？点击进入首页</el-button>
            <el-button @click="logout" style="width:130px" type="danger" plain>注销</el-button>
          </template>

        </div>
      </div>
    </div>
  </div>


</template>


<script>
  import axios from 'axios'
  import qs from 'qs'
  axios.defaults.withCredentials = true;

  export default {

    mounted() {
      localStorage.setItem('ip','http://192.168.16.31:8080')
      this.ip = localStorage.getItem('ip');
      this.token = sessionStorage.getItem('token')
    },

    name: 'Main',
    data() {
      return {
        logIP:'http://192.168.16.31:8080',
        ip:'',

        accountType:'学号',
        backgroundColor:'',
        token:'',
        input: '',
        // token: '',
        formLabelAlign: {
          username: '',
          password: '',
          identity: 'STUDENT'
        }
      };
    },
    methods: {
      selectStudent(){//切换登录角色为学生(默认为学生)
        this.accountType='学号'
        this.$refs.xuesheng.style.backgroundColor='#6F6F6FFF'
        this.$refs.jiaoshi.style.backgroundColor='transparent'
        this.$refs.guanliyuan.style.backgroundColor='transparent'
        this.formLabelAlign.identity='STUDENT'
      },
      selectTeacher(){//切换登录角色为教师
        this.accountType='工号'
        this.$refs.xuesheng.style.backgroundColor='transparent'
        this.$refs.jiaoshi.style.backgroundColor='#6F6F6FFF'
        this.$refs.guanliyuan.style.backgroundColor='transparent'
        this.formLabelAlign.identity='TEACHER'
      },
      selectAdmin(){//切换登录角色为管理员
        this.accountType='工号'
        this.$refs.xuesheng.style.backgroundColor='transparent'
        this.$refs.jiaoshi.style.backgroundColor='transparent'
        this.$refs.guanliyuan.style.backgroundColor='#6F6F6FFF'
        this.formLabelAlign.identity='ADMIN'
      },
      test(){//检测按钮(发送token到服务器，检测是否已登录)
        axios.defaults.withCredentials = true;
        axios.defaults.headers.common["token"] = this.token;
        axios.get(this.ip + '/api/getMessage',{withCredentials : true})
          .then(function(response){
          console.log(response.data)
          console.log(response)
        }).catch(function (error) {
          console.log(error);
        });
      },
      submit() {//登录按钮执行函数
        axios.defaults.withCredentials = true;
        axios.post(this.logIP + '/login',
         qs .stringify(this.formLabelAlign), {headers: {'Content-Type':'application/x-www-form-urlencoded'}}
        ).then((response)=>{
          console.log(response.data)
          console.log(response)

          if(response.data.code === 200){//根据返回数据中的code值，判断是否登录成功
            console.log(response.data.token)
            let id = response.data.user.identity
            sessionStorage.setItem('token', response.data.token)
            sessionStorage.setItem('identity', id)
            this.token = sessionStorage.getItem('token')

            if(this.formLabelAlign.identity === 'STUDENT'){
              window.location.href="/home.html"
            }
            else if(this.formLabelAlign.identity === 'TEACHER') {
              window.location.href="/admin.html"
            }
            else if(this.formLabelAlign.identity === 'ADMIN'){
              window.location.href="/admin.html"
            }

          }else if(response.data.code === 400){
            alert('登录失败!')
          }
        }).catch(function (error) {
          console.log(error.message);
        });
      },
      clearInput(){
        this.formLabelAlign.username = '';
        this.formLabelAlign.password = '';
      },
      enterMain(){
        window.location.href='/home.html'
      },
      logout() {
        sessionStorage.removeItem('token');
        sessionStorage.removeItem('identity');
        window.location.href='/sign.html'
      }
    },


  }
</script>


<style>
  .search-query {
    width:450px;
    height: 30px;
    line-height: 30px;
    box-sizing: border-box;
    padding: 0 15px 0 30px;
    border: 2px solid #e3e3e3;
    color: #273849;
    outline: none;
    border-radius: 15px;
    margin-right: 10px;
    transition: border-color 0.2s ease;
    background: #fff url("../pages/home/assets/search.png") 8px 5px no-repeat;
    background-size: 20px;
    vertical-align: middle !important;
  }
  .search-query:focus {
    border-color: rgba(5, 161, 206, 0.99);
  }

  .body{
    display: flex;
    flex-direction:row;
    justify-content:flex-end;
    align-items: center;
    background-color:#f0f1f9;
    min-height:1000px;
    padding:0;
    background-image: url(../pages/sign/assets/signBG.jpg);
    background-size: 1984px 1038px;
  }
  .loginBox{
    display: flex;
    flex-direction: column;
    width:550px;
    height:400px;
    margin-right: 80px;
    background-color:rgb(249, 252, 254);
  }
  .userSelect{
    display: flex;
    flex-direction:row;
    justify-content:space-between;
    width:auto;
    height: 60px;
    margin:5px;
    background-color: rgba(210, 209, 209, 0.79);
  }
  .userBlock{
    height: 60px;
    width:163px;
    font-size:23px;
    text-align: center;
    line-height: 55px;

  }
  .userBlock:hover{
    background-color: #6F6F6FFF;
  }
  .loginForm{
  }
  .el-form-item__label{
    font-size: 25px;
  }
  .inputBox{
    margin-top:50px;
    margin-bottom: 50px;
    margin-left:30px;
  }
  .el-form-item__content{
    margin-right: 50px;
    margin-left:30px;
  }
  .summitBox{
    display: flex;
    justify-content: space-evenly;
    padding:10px 0 10px 0;
    height: 50px;
  }
  .el-input__inner{
    width:330px;
    height:45px;
    font-size: 25px
  }
  .el-button{
    width:100px;
    font-size: 25px;
  }


  a{
    text-decoration: none;
    color:black;
  }


</style>
